package com.demo.studentmanager;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.demo.studentmanager.databinding.ItemStudnetBinding;

import java.util.List;

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.VH> {
    public StudentAdapter(List<User> userList) {
        this.userList = userList;
    }

    private List<User> userList;

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new VH(ItemStudnetBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        User user = userList.get(position);
        holder.binding.etPhone.setText(user.getAccount());
        holder.binding.etAccount.setText(user.getName());
        holder.binding.etClass.setText(user.getClazz());
        holder.binding.etSex.setText(user.getSid());
        holder.binding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = v.getContext();
                context.startActivity(new Intent(context,EditStudentInfoActivity.class)
                        .putExtra("self",false)
                        .putExtra("phone",user.getAccount()));
            }
        });
    }

    @Override
    public int getItemCount() {
        return userList.size();
    }

    public static class VH extends RecyclerView.ViewHolder {
        ItemStudnetBinding binding;

        public VH(@NonNull ItemStudnetBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
